#include "EntityMesh.h"


EntityMesh::EntityMesh(void)
{
	shader = new Shader();
	shader->load("textureShader.vs","textureShader.ps");
}


EntityMesh::~EntityMesh(void)
{

}

void EntityMesh::render(){
	GLuint tex_id = texture->texture_id;
	
	shader->enable();
	glPushMatrix();
	model.set();
	texture->bind();
	
	shader->setUniform1("tex", (int)0);
	shader->setUniform3("lightDir", 1.0f, 0.0f, 1.0f);
	mesh->render();
	shader->disable();
	texture->unbind();
	for(unsigned int i = 0; i < children.size(); ++i){
		children[i]->render();
	}
	
	
	glLineWidth(2.0);
	
	glBegin(GL_LINES);
		glColor3f(1,1,1);
		Vector3 center = model*Vector3();
		Vector3 point2 = model*Vector3(0,-100,0);
		glVertex3f(center.x,center.y,center.z);
		glColor3f(1,1,1);
		//Vector3 lineEnd = (model*speed).normalize()*-10;
		glVertex3f(point2.x, point2.y, point2.z);
	glEnd();
	glPopMatrix();

}

bool EntityMesh::collisions(Mesh* collides)
{
       
       float pointCollision[3], t1[9],t2[9];
       if(mesh->collision_model->collision(collides->collision_model,-1,0,0)==true){//////
               std::cout << "PointSpitfireContact: " << std::endl;
               mesh->collision_model->getCollisionPoint(pointCollision,true);//////
               mesh->collision_model->getCollidingTriangles(t1,t2,true);
               for(int i=0;i<3;i++) std::cout << pointCollision[i] << std::endl;
			   return true;
       }
	   return false;
}

bool EntityMesh::rayCollision(Vector3 ray_origin, Vector3 ray_direction){
	return this->mesh->collision_model->rayCollision(ray_origin.v, ray_direction.v,false);

}

bool EntityMesh::segmentCollision(Vector3 ray_origin, Vector3 ray_direction, float t0, float t1){
	return this->mesh->collision_model->rayCollision(ray_origin.v, ray_direction.v, false, t0, t1);

}
